﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using ServiceStack.Redis;

namespace Epona.AOP.Cache
{
    internal class RedisCacheManager
    {
        private static RedisCacheManager instance;

        private static object lockObject = new object();

        public PooledRedisClientManager Manager;

        //public double CacheTime = 0.0;

        private RedisCacheManager()
        {
            RedisClientManagerConfig config = new RedisClientManagerConfig();
            config.MaxReadPoolSize = Convert.ToInt32(ConfigurationManager.AppSettings["RedisCacheMaxReadPoolSize"]);
            config.MaxWritePoolSize = Convert.ToInt32(ConfigurationManager.AppSettings["RedisCacheMaxWritePoolSize"]);
            config.AutoStart = true;

            string cacheServerIP = ConfigurationManager.AppSettings["RedisCacheServerIP"];
            Manager = new PooledRedisClientManager(new List<string>() { cacheServerIP }, new List<string>() { cacheServerIP }, config);

            //CacheTime = Convert.ToDouble(ConfigurationManager.AppSettings["RedisCacheTimeSpan"]);
        }

        public static RedisCacheManager RedisCacheManagerSingleton()
        {
            if (instance == null)
            {
                lock (lockObject)
                {
                    if (instance == null)
                    {
                        instance = new RedisCacheManager();
                    }
                }
            }

            return instance;
        }
    }
}
